package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.entity.PlayRoleBookImage;
import la.iok.hzsvn.lewin.movie.mapper.PlayRoleBookImageMapper;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

@Service
public class PlayRoleBookImageServiceImpl extends BaseServiceImpl<PlayRoleBookImage, PlayRoleBookImageMapper,Long>
        implements PlayRoleBookImageService {
    private final PlayRoleBookFileService playRoleBookFileService;
    public PlayRoleBookImageServiceImpl(PlayRoleBookImageMapper mapper,
                                        PlayRoleBookFileService playRoleBookFileService) {
        super(mapper);
        this.playRoleBookFileService = playRoleBookFileService;
    }

    @Override
    public Long save(@NotNull Long playRoleBookInstanceId, @NotNull Long fileId, @NotNull String filePath) {
        PlayRoleBookImage entity = new PlayRoleBookImage();
        entity.setPlayRoleBookInstanceId(playRoleBookInstanceId);
        entity.setFilePath(filePath);
        entity.setFileId(fileId);
        insertSelective(entity);
        return entity.getId();
    }

    @Nullable
    @Override
    public String digest(@Nullable Long id) {
        PlayRoleBookImage img = select(id);
        if(img == null){
            return null;
        }
        return playRoleBookFileService.digest(img.getFileId());
    }

    @Nullable
    @Override
    public String storePath(@Nullable Long id) {
        PlayRoleBookImage img = select(id);
        if(img == null){
            return null;
        }
        return playRoleBookFileService.storePath(img.getFileId());
    }

    @Override
    public void markFileDeleted(@Nullable @NullElement Collection<Long> ids) {
        batchUpdate("fileDeleted",1, ids);
    }

    @Override
    public void downloadFile(@NotNull HttpServletResponse response, @NotNull Long id) {
        PlayRoleBookImage image = select(id);
        if(image == null){
            return;
        }
        playRoleBookFileService.downloadFile(response,image.getFileId(),String.valueOf(id));
    }
}
